#!/usr/bin/env bash

#
# comment here
#

# "$@" = "$1" "$2" "$3" ... "$n"
# "$*" = "$1y$2y$3y...$n", where y is the value of IFS variable i.e. 
# "$*" is one long string and $IFS act as an separator or token delimiters.
# Use Uppercase Names for ENVIRONMENT Variables and Lowercase for Custom Variables

# IFS='\n\t' # Default IFS for “zsh” is: $' \n\t\0'.

set -euo pipefail
# set -o xtrace

declare -r script_path="${BASH_SOURCE[0]}"
# declare -r script_dir="${script_path%/*}"
declare -r script_file="${script_path##*/}"

# path="dir/basename.ext"
# dir="${path%/*}"
# filename="${path##*/}" # basename.ext
# basename="${path%\.*}"
# ext=${path##*.}


info() { printf "\\e[1m\\e[38;5;14m✔ %s\\e[0m\\n" "$@"; }
warn() { printf "\\e[1m\\e[38;5;148m➜️ %s\\e[0m\\n" "$@"; }
error(){ printf "\\e[1m\\e[38;5;196m✖ %s\\e[0m\\n" "$@"; }

die() {
    local last_error=$?
    if [[ $1 =~ ^[0-9]+$ ]]; then
        last_error=$1
        shift
    fi
    error "${script_file}:${BASH_LINENO[0]} ${FUNCNAME[1]}(${last_error})> $*" >&2
    exit "$last_error"
}

#declare -i # for integer
#declare -a # for array
#declare -r # readonly

base_dir="/Users/wangchao9/projects/android/xalan-j/src/org/apache"
target_dir="/Users/wangchao9/projects/game/jx/jx-editor/app/src/main/java/wx/xml/xalan"

if [[ $# -lt 1 ]]; then
    echo "Usage:${script_file} class-name"
    exit 1
fi

process_file(){
    if [[ -z "$1" ]]; then
        die "empty query"
    fi

    class_name=$(echo "$1" | sed 's#\.#/#g')
    relative_filename="${class_name}.java"
    # echo "relative_filename:$relative_filename"
    if [[ ! -e "$base_dir/${relative_filename}" ]]; then
        #info "$base_dir/${relative_filename} not exist"
        #info "try to find ${relative_filename}"
        declare -r class_name_file="${relative_filename##*/}"
        
        found=$(find "$base_dir" -name "${class_name_file}")
        if [[ -z "$found" ]]; then
            die "no match $class_name_file"
        fi

        #echo "found:$found"
        line_number=$(echo "$found" | wc -l)
        #echo "line_number: $line_number"
        if [[ ! "${line_number}" -eq 1 ]]; then
            warn "too many match" 
            echo "$found"
            exit 0
        fi
        
        if [[ -n $found ]]; then
            relative_filename=$(echo "$found" | sed "s#${base_dir}/##g")
            #info "relative_filename:$relative_filename"
        else
            :#info "found: $found"
        fi
    fi 

    if [[ ! -e "$base_dir/${relative_filename}" ]]; then
        die "$base_dir/${relative_filename} not exist"
    fi
    
    #echo "copy and replace:${relative_filename}"
    target_path="$target_dir/${relative_filename}"
    declare -r target_path_dir="${target_path%/*}"
    mkdir -p "${target_path_dir}"
    cp "$base_dir/${relative_filename}" "${target_path}" || die "copy failed"
    sed -i '' 's/org\.apache/wx\.xml\.xalan/g' "${target_path}" || die "replace failed"
}

main(){
    #echo "input: $*"
    for i in "$@";do
        #echo "process: $i"
        process_file "$i"
    done 
}

main "$@"